﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Interfaces;
using System.ComponentModel;

namespace SmartConference.Api.Applocations.Services
{
    public class DepartmentServices : IDepartmentServices
    {
        private readonly IDepartmentRepository departmentRepository;
        private readonly IMapper mapper;

        public DepartmentServices(IDepartmentRepository departmentRepository,IMapper mapper)
        {
            this.departmentRepository = departmentRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> DeleteDept(int DepartmentId)
        {
            try
            {
                var deptlist = await departmentRepository.GetAsync(x => x.DepartmentId == DepartmentId);
                await departmentRepository.DeleteAsync(deptlist);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult<List<ItemMenu>>> GetDeptByParentId(int ParentId)
        {
            try
            {
                var deptlist=await GetDepartmentByParentId(ParentId);
                return ApiResult<List<ItemMenu>>.Success(ApiResultCode.Success, deptlist);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<List<ItemMenu>> GetDepartmentByParentId(int parentid)
        {
            var list = departmentRepository.GetAll(x => x.ParentId == parentid).ToList();
            List<ItemMenu> newlist = new List<ItemMenu>();
            foreach (var item in list)
            {
                newlist.Add(new ItemMenu()
                {
                    label = item.DepartmentName,
                    value = item.DepartmentId,
                    children = await GetDepartmentByParentId(item.DepartmentId)
                });
            }
            return newlist;
        }

        public async Task<ApiResult<IList<TreeDept>>> ShowDepartment(string? DepartmentName, int parentid)
        {
            try
            {
                var list = await GetTreeById(parentid);
                return ApiResult<IList<TreeDept>>.Success(ApiResultCode.Success, list);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<IList<TreeDept>> GetTreeById(int parentid)
        {
            var list = departmentRepository.GetAll(x => x.ParentId == parentid).ToList();
            IList<TreeDept> newlist=mapper.Map<IList<TreeDept>>(list);
            foreach (var item in newlist)
            {
                item.Children = (System.Collections.IList)await GetTreeById(item.DepartmentId);
            }
            return newlist;
        }


        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="departmentInfo">部门实体</param>
        /// <returns></returns>
        public async Task<ApiResult> AddDept(AddDepartmentDTO departmentInfoDTO)
        {
            try
            {
                var deptlist = await departmentRepository.GetAsync(x => x.DepartmentName == departmentInfoDTO.DepartmentName && !x.IsDelete);
                if (deptlist != null)
                {
                    return ApiResult.Error(ApiResultCode.Error, "部门已存在");
                }
                var departmentInfo = mapper.Map<DepartmentInfo>(departmentInfoDTO);
                await departmentRepository.AddAsync(departmentInfo);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="departmentInfo">部门实体</param>
        /// <returns></returns>
        public async Task<ApiResult> UpdateDept(DepartmentInfo departmentInfo)
        {
            try
            {
                var deptlist =await departmentRepository.GetAsync(x => x.DepartmentId != departmentInfo.DepartmentId&&x.DepartmentName==departmentInfo.DepartmentName&&!x.IsDelete);
                if (deptlist != null)
                {
                    return ApiResult.Error(ApiResultCode.Error, "部门已存在");
                }
                await departmentRepository.UpdateAsync(departmentInfo);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改部门状态
        /// </summary>
        /// <param name="DepartmentId">部门Id</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public async Task<ApiResult> UpdateDeptStatus(int DepartmentId, int status)
        {
            try
            {
                var deptlist=await departmentRepository.GetAsync(x => x.DepartmentId == DepartmentId);
                if (status == 0)
                {
                    deptlist.DepartmentStatus = (DepartmentStatus)status;
                }
                else
                {
                    deptlist.DepartmentStatus = (DepartmentStatus)status;
                }
                await departmentRepository.UpdateAsync(deptlist);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
